<?php
// +----------------------------------------------------------------------
// | 会员卡数据处理
// +----------------------------------------------------------------------
// | Author: TongBC <501468940@qq.com>
// +----------------------------------------------------------------------
// | Date  : 2015/03/06
// +----------------------------------------------------------------------
include_once '/var/www/html/new/com.inc.php';
include_once '/var/www/html/new/d/common/func.inc.php';
include_once '/var/www/html/new/d/class/MemberAccount.class.php';
include_once '/var/www/html/new/d/class/abc/Tools.class.php';
include_once '/var/www/html/new/d/class/abc/Validate.class.php';
include_once '/var/www/html/new/d/module/pft_show/PftShow.class.php';

include_once '/var/www/html/new/d/module/common/Db.class.php';
$dbConf = include '/var/www/html/new/d/module/common/db.conf.php';// 远端服务器配置信息

use \PFT\Db;
Db::Conf($dbConf['remote_1']);

// $ac=$GLOBALS['_ac'];
// $pw=$GLOBALS['_pw'];
// $s = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));

if(!isset($_SESSION['sid'])) exit('{"status":"fail","msg":"登陆超时","code":0}');


switch(trim($_REQUEST['action'])) {
	
	
	// 场馆、分区数据处理
	case 'venues':
	
		$recvData = _addslashes($_REQUEST);// 接收数据
		// print_r($recvData);
		// exit;
		$venue_id = $recvData['venue_id'] = $recvData['venue_id'] + 0;
		if($recvData['venue_id']==0){// 新增场馆
			if($recvData['venue_name']=='') exit('{"status":"fail","msg":"场馆名称不能为空"}');
			
			// 名称唯一性
			$sql = "select id from pft_venues where venue_name='{$recvData['venue_name']}' and apply_did={$_SESSION['sid']} and status=0 limit 1";
			if(Db::fetch($sql)) exit('{"status":"fail","msg":"场馆名称已存在"}');
			
			$save['status']      = 0;
			$save['apply_did']   = $_SESSION['sid'];
			$save['venue_name']  = $recvData['venue_name'];
			$save['venue_thumb'] = $recvData['venue_thumb'];
			$sql = _insertSqlBuild($save, 'pft_venues');
			$venue_id = (Db::execute($sql)) ? Db::lastInsertId():0;
		}else{// 更新
			$sql = "select id from pft_venues where venue_name='{$recvData['venue_name']}' order by id desc limit 1";
			$dbRow = DB::fetch($sql);
			if($dbRow && $dbRow['id']!=$venue_id)exit('{"status":"fail","msg":"场馆名称已存在"}');
			$sql = "update pft_venues set venue_name='{$recvData['venue_name']}',venue_thumb='{$recvData['venue_thumb']}' where id=$venue_id limit 1";
			Db::execute($sql);
		}

		if($venue_id==0) exit('{"status":"fail","msg":"参数出错"}');
		
		// 分区操作
		if(isset($recvData['area_id']) && count($recvData['area_id'])){
			foreach($recvData['area_id'] as $key=>$area_id){
				
				$area_name  = $recvData['area_name'][$key];
				$isDelete   = $recvData['isDelete'][$key];// 1  添加 0 删除

				if($area_id>0){// 存在数据
					if($isDelete==0){// 删除
						$sql = "delete from pft_roundzone where id=$area_id and venue_id=$venue_id limit 1";
						Db::execute($sql);continue;
					}
					$sql = "update pft_roundzone set zone_name='$area_name' where id=$area_id and venue_id=$venue_id limit 1";
					Db::execute($sql);continue;
				}
				
				// 新增分区
				$sql = "select id from pft_roundzone where venue_id=$venue_id and zone_name='$area_name' order by id desc limit 1";
				if(!Db::fetch($sql)){
					$ins = "insert into pft_roundzone set venue_id=$venue_id,zone_name='$area_name'";
					Db::execute($ins);
				}
			}
		}
		
		exit('{"status":"success","msg":"保存成功","venue_id":'.$venue_id.'}');
		break;
	// 分区生成座位信息
	case 'set_seat':
		$recvData = json_decode($_REQUEST['data'], true);
		$venue_id    = $recvData['venue_id'] + 0;
		$seat_row    = $recvData['row_num'] + 0;
		$seat_col    = $recvData['col_num'] + 0;
		$seat_layout = $recvData['layout'] + 0;
		$col_side    = $recvData['row_head'];
		$col_head    = $recvData['col_head'];
		$sql = "update pft_venues set row_num=$seat_row,col_num=$seat_col,layout=$seat_layout,col_side='$col_side',col_head='$col_head' where id=$venue_id limit 1";
		if(!Db::execute($sql)) exit('{"status":"fail","msg":"场馆信息更新失败"}');
		// echo $sql;
		// print_r($recvData);
		
		// 获取场馆座位信息
		// $set
		$sql = "select * from pft_roundseat where venue_id=$venue_id";
		$dbRows = Db::fetchAll($sql);
		foreach($dbRows as $key=>$row){
			$row_col = $row['row_num'].$row['col_num'];
			$dbSetArr[$row_col] = $row;
		}

		$upd = "update pft_roundseat set seat_status=1 where venue_id=$venue_id";
		Db::execute($upd);
		
		// 外层分区
		foreach($recvData['areas'] as $key=>$area){
			$area_id  = $area['zone_id'];// 分区ID
			foreach($area['seats'] as $k=>$seat){
				$custom_num = $seat['seat'];
				$row_col = $seat['row_num'].$seat['col_num'];
				if(isset($dbSetArr[$row_col])){// 存在相同座位号
					$round_id = $dbSetArr[$row_col]['id'];
					$upd = "update pft_roundseat set zone_id=$area_id,custom_num='$custom_num',seat_status=0 where id=$round_id limit 1";
					Db::execute($upd); continue;
				}else{// 新增
					$ins = "insert into pft_roundseat set venue_id=$venue_id, zone_id=$area_id,custom_num='$custom_num',col_num={$seat['col_num']},row_num={$seat['row_num']}";
					Db::execute($ins);
				}
			}
		}
		
		exit('{"status":"success","msg":"保存成功"}');
		// print_r($_REQUEST);
		break;
	// 删除场馆
	case 'remove_venue':
		$venue_id = $_REQUEST['venue_id']+0;
		$showCls = new PFT\Show\PftShow(Db::Connect());
		$result  = $showCls->UpdateVenues($venue_id,1);
		if($result['status']=='ok') exit('{"status":"success","msg":"删除成功"}');
		exit('{"status":"fail","msg":"删除失败"}');
		break;
	// 获取场馆列表
	case 'get_venue_list':
		// $showCls = new PFT\Show\PftShow(Db::Connect());
		// $result  = $showCls->GetVenuesName($_SESSION['sid']);
		$sql = "SELECT id as venue_id,venue_name,venue_thumb FROM pft_venues WHERE apply_did=? AND status=?";
        $stmt= Db::Connect()->prepare($sql);
        $stmt->execute(array($_SESSION['sid'], 0));
        $venues = array();
        while($row=$stmt->fetch(\PDO::FETCH_ASSOC)) {
            $venues[] = $row;
        }
		
		$response = array(
			'status' => 'success',
			'data'   => $venues
		);
		exit(json_encode($response));
		break;
	// 获取场馆信息
	case 'get_venue_seat':
		$venue_id = $_REQUEST['venue_id']+0;
		if($venue_id==0) exit('{"status":"fail","msg":"参数错误"}');
		$var['venue_id']  = $venue_id;
		$var['venue_name'] = '';
		$var['col_num']  = 20;
		$var['row_num']  = 8;
		$var['layout'] = 1;
		$var['areas'] = array();
		// 获取场馆信息
		$sql = "select id as venue_id,venue_name,col_num,row_num,layout,col_side as row_head,col_head from pft_venues where id=$venue_id limit 1";
		$dbRow = Db::fetch($sql);
		if(is_array($dbRow)) $var = array_merge($var, array_filter($dbRow));
		
		// 获取分区
		$sql = "select id as zone_id,zone_name from pft_roundzone where venue_id=$venue_id";
		$dbRow = Db::fetchAll($sql);
		if(is_array($dbRow)) $var['areas'] = $dbRow;
		
		foreach($var['areas'] as $key=>$val){// 获取座位
			$sql = "select id,col_num,row_num,custom_num as seat from pft_roundseat where venue_id=$venue_id and zone_id={$val['zone_id']} and seat_status=0";
			$var['areas'][$key]['seats'] = Db::fetchAll($sql);
		}
		exit(json_encode($var));
		break;
    default:break;
	
}

// 数据转义
function _addslashes($data){
	if(is_array($data)){
		foreach($data as $key=>$value){
			$data[$key] = _addslashes($value);
		}
		return $data;
	}
	return addslashes($data);
}

// 生成语句
function _insertSqlBuild($data, $table){
	if(is_null($data)) return '';
	$sql = 'insert into `'.$table.'` set ';
	foreach($data as $field=>$value){
		$sql = $sql.'`'.$field.'`="'.$value.'",';
	}
	return rtrim($sql,',');
}
// 生成编辑语句
function _updateSqlBuild($data, $table){
	if(is_null($data)) return '';
	$sql = 'update `'.$table.'` set ';
	foreach($data as $field=>$value){
		$sql = $sql.'`'.$field.'`="'.$value.'",';
	}
	$sql = rtrim($sql,',');
	return $sql;
}
?>